<?php

use yii\db\Migration;

/**
 * Handles the creation of table `{{%page_redirect}}`.
 * Has foreign keys to the tables:
 *
 * - `{{%page}}`
 * - `{{%page}}`
 */
class m230629_051753_create_page_redirect_table extends Migration
{
    /**
     * {@inheritdoc}
     */
    public function safeUp()
    {
        $this->createTable('{{%page_redirect}}', [
            'id' => $this->primaryKey(),
            'parent_id' => $this->integer()->notNull()->unique(),
            'child_id' => $this->integer()->notNull(),
        ]);

        // creates index for column `parent_id`
        $this->createIndex(
            '{{%idx-page_redirect-parent_id}}',
            '{{%page_redirect}}',
            'parent_id',
            true
        );

        // add foreign key for table `{{%page}}`
        $this->addForeignKey(
            '{{%fk-page_redirect-parent_id}}',
            '{{%page_redirect}}',
            'parent_id',
            '{{%page}}',
            'id',
            'CASCADE'
        );

        // creates index for column `child_id`
        $this->createIndex(
            '{{%idx-page_redirect-child_id}}',
            '{{%page_redirect}}',
            'child_id'
        );

        // add foreign key for table `{{%page}}`
        $this->addForeignKey(
            '{{%fk-page_redirect-child_id}}',
            '{{%page_redirect}}',
            'child_id',
            '{{%page}}',
            'id',
            'CASCADE'
        );
    }

    /**
     * {@inheritdoc}
     */
    public function safeDown()
    {
        // drops foreign key for table `{{%page}}`
        $this->dropForeignKey(
            '{{%fk-page_redirect-parent_id}}',
            '{{%page_redirect}}'
        );

        // drops index for column `parent_id`
        $this->dropIndex(
            '{{%idx-page_redirect-parent_id}}',
            '{{%page_redirect}}'
        );

        // drops foreign key for table `{{%page}}`
        $this->dropForeignKey(
            '{{%fk-page_redirect-child_id}}',
            '{{%page_redirect}}'
        );

        // drops index for column `child_id`
        $this->dropIndex(
            '{{%idx-page_redirect-child_id}}',
            '{{%page_redirect}}'
        );

        $this->dropTable('{{%page_redirect}}');
    }
}
